import React, { lazy } from 'react';
import { AppstoreOutlined, SettingOutlined } from '@ant-design/icons';

// 配置路由和菜单列表
export const allRouteList: any[] = [
  {
    path: '/home',
    name: 'home',
    component: lazy(() => import('@/pages/Home')),
  },
  {
    path: '/report',
    name: 'report',
    component: lazy(() => import('@/pages/Report')),
  },
  {
    path: '/demo',
    name: 'demo',
    component: lazy(() => import('@/pages/Demo')),
  },
  {
    path: '/demo/detail',
    name: 'demoDetail',
    component: lazy(() => import('@/pages/Demo/Detail')),
  },
  {
    path: '/contactUs',
    name: 'contactUs',
    component: lazy(() => import('@/pages/ContactUs')),
  },

  {
    path: '/map',
    name: 'map',
    component: lazy(() => import('@/pages/Test/Map')),
  },
];

// 路由列表-数组扁平化
interface RouteType {
  path: string;
  component?: React.ReactNode;
  children?: RouteType;
  hideInMenu?: boolean;
  [key: string]: any;
}

// 路由列表扁平化-一维数组
function getFlatRoutes(routeList: RouteType[] = []): RouteType[] {
  const routes: any = []; // 所有路由
  routeList.forEach((item: any) => {
    const { path, component, children, hideInMenu } = item;
    const routeObj: RouteType = { path };
    if (hideInMenu) routeObj.hideInMenu = true;
    if (component) {
      routeObj.element = component;
      routes.push(routeObj);
    }
    if (children?.length) {
      routes.push(...getFlatRoutes(children));
    }
  });
  return routes;
}

// 生成路由列表-数组扁平化
export function createRoutes(paths: any = [], ISAUTH = false) {
  const flatRoutes = getFlatRoutes(allRouteList);
  // 判断是否需要权限
  const routes = !ISAUTH
    ? flatRoutes
    : flatRoutes.filter((item: any) => paths.includes(item?.path) || item?.hideInMenu);
  // console.log('routes: ', routes);
  return routes;
}
